package com.ltu.controller.customize;

import java.util.List;

import org.apache.logging.log4j.core.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltu.model.request.base.BaseFilterPageReq;
import com.ltu.model.request.base.BaseIdReq;
import com.ltu.model.response.base.CodeDataResp;
import com.ltu.service.customize.SessionInfoService;
import com.ltu.service.customize.SessionRecordService;
import com.ltu.service.impl.BaseServiceImpl;
import com.ltu.util.common.MpPageUtil;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.ltu.domain.mp_entity.customize.SessionInfoEntity;
import com.ltu.domain.mp_entity.customize.SessionRecordEntity;
import com.ltu.mapper.customize.SessionRecordMapper;


/**
 * <p>
 * 会话详情 前端控制器
 * </p>
 *
 * @author 若尘
 * @since 2024-10-17
 */
@RestController

@RequestMapping("/SessionRecord")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags= "会话详情")
public class SessionRecordController extends BaseServiceImpl<SessionRecordMapper, SessionRecordEntity>{
	private final SessionInfoService  sessionInfoService;
	
	
	
    @ApiOperation(value="获取分页列表")
    @RequestMapping(value="/getMePage", method= RequestMethod.POST)
    public CodeDataResp<Page<SessionRecordEntity>> getMePage(@RequestBody  BaseFilterPageReq<SessionRecordEntity> req ) {
    	Page<SessionRecordEntity> page = MpPageUtil.getCommonPage(req.getPageReq());
		QueryWrapper<SessionRecordEntity>  t=new QueryWrapper<SessionRecordEntity>();
		
		if(null != req.getKeyWords()&& req.getKeyWords().size() > 0) 
			t.apply(getLikeStr(req.getKeyWords(),""));
		 t.setEntity(req.getEntityParam());
		 Page<SessionRecordEntity>	 result= super.page(page, t);
		return CodeDataResp.valueOfSuccess(result);
    }
    
    @ApiOperation(value="获取列表")
    @RequestMapping(value="/getList", method= RequestMethod.POST)
    public CodeDataResp<List<SessionRecordEntity>> getList(@RequestBody  BaseFilterPageReq<SessionRecordEntity> req ) {
        return super.getMeList(req);
    }
    
    @ApiOperation(value="获取某个Session会话详情")
    @RequestMapping(value="/getBySessionId", method= RequestMethod.POST)
    public CodeDataResp<SessionInfoEntity> getBySessionId(@RequestParam  Integer sessionId ) {
    	SessionInfoEntity  session =	this.sessionInfoService.getById(sessionId);
    	Assert.requireNonEmpty(session);
    	QueryWrapper<SessionRecordEntity>  qw = new QueryWrapper<SessionRecordEntity>();
    	qw.lambda().eq(SessionRecordEntity :: getSessionId, sessionId);
    	List<SessionRecordEntity> records =	this.list(qw);
    	session.setList( records);
    	return CodeDataResp.valueOfSuccess(session);
    }
    
    
    @ApiOperation(value="标记会话类型并获取下一个未标记的会话")
    @RequestMapping(value="/markSession", method= RequestMethod.GET)
    public CodeDataResp<SessionInfoEntity> markSession(@RequestParam  Integer sessionId,@RequestParam  String markStr ,@RequestParam  Integer sessionFileId) {
    	SessionInfoEntity  session =	this.sessionInfoService.getById(sessionId);    	
    	Assert.requireNonEmpty(session);
    	session.setSessionField(markStr);
    	session.setSessionFieldFlag(1);
    	session.setSessionFieldId(sessionFileId);
    	this.sessionInfoService.updateById(session);
    	
    	QueryWrapper<SessionInfoEntity> qw2 = new QueryWrapper<SessionInfoEntity>();
    	qw2.orderByDesc("id");
    	qw2.lambda().eq(SessionInfoEntity :: getSessionFieldFlag, 0)
    				.last(" limit 1 ")
    				;
    	SessionInfoEntity  session2 =	this.sessionInfoService.getOne(qw2);
    
    	QueryWrapper<SessionRecordEntity>  qw = new QueryWrapper<SessionRecordEntity>();
    	qw.lambda().ne(SessionRecordEntity :: getSessionId, session2.getId());
    	List<SessionRecordEntity> records =	this.list(qw);
    	session2.setList( records);
    	return CodeDataResp.valueOfSuccess(session2);
    }
    
    

    @ApiOperation(value="新增/保存")
    @RequestMapping(value="/edit", method= RequestMethod.POST)
    public CodeDataResp edit(@RequestBody SessionRecordEntity req) {
        return super.edit(req);
    }
    
//    @ApiOperation(value="批量修改数据")
//    @RequestMapping(value="/edits", method= RequestMethod.POST)
    public CodeDataResp edits(@RequestBody List<SessionRecordEntity> reqs) {
    	super.saveOrUpdateBatch(reqs);
    	return   CodeDataResp.valueOfSuccessEmptyData();
    }
    
    
    	@ApiOperation(value="获取详情")
    @RequestMapping(value="/getDetail/{id}", method= RequestMethod.GET)
    public CodeDataResp<SessionRecordEntity> getDetail(@PathVariable String id) {
 		BaseIdReq req=new BaseIdReq(id);
        return super.getDetail(req);
    }
    

 	 @ApiOperation(value="删除")
     @RequestMapping(value="/remove/{id}", method= RequestMethod.GET)
     public CodeDataResp remove(@PathVariable String id) {
     	BaseIdReq req=new BaseIdReq(id);
     	return super.remove(req);
     }
     
 //   @ApiOperation(value="删除s")
//    @RequestMapping(value="/removes", method= RequestMethod.POST)
    public CodeDataResp removes(@RequestBody List<Integer> ids) {
    		super.removeByIds(ids);
        return CodeDataResp.valueOfSuccessEmptyData();
    }

}

